/**
 * Activity to call the service
 * 
 * Remember:
 * When a service is unbound from all clients, the Android system destroys it 
 * (unless it was also started with onStartCommand()).
 * 
 * SVN TAG:
 * "@(#) $Id: xxx.y 143715 2010-11-17 15:16:19Z wzbhsz $ $HeadURL: https://rzb-svn-dev.at.rzb.rbg.cc/systems/RZB_CAD/DB/trunk/cad/src/cad_calc.c $"
 */

package at.hockey.ticker;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class HTActivity extends Activity implements OnClickListener {
	static final String SEND_GAME_NOTIFICATIONS = "at.hockey.ticker.SEND_GAME_NOTIFICATIONS";
	static final String SEND_SVCSTAT_NOTIFICATIONS = "at.hockey.ticker.SEND_SVCSTAT_NOTIFICATIONS";
	
	
	private final String TAG = this.getClass().getSimpleName();
    Button mButtonStart, mButtonStop;
	TextView mTVServiceStatus, mTVDataStatus;

	HTApplication HTapp;
	HTReceiverDat mReceiverDat;
	HTReceiverStat mReceiverStat;
	
	IntentFilter filterDat, filterStat;
	
	public String mDataBoxData;
	public String mServiceStatus;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//	    Log.i(TAG, "onCreated");
        setContentView(R.layout.main);

        mButtonStart = (Button) findViewById(R.id.buttonStart);
        mButtonStop = (Button) findViewById(R.id.buttonStop);
        
        mButtonStart.setOnClickListener(this);
        mButtonStop.setOnClickListener(this);

        mTVServiceStatus = (TextView) findViewById(R.id.serviceStatusId);
        mTVDataStatus = (TextView) findViewById(R.id.dataStatusId);
        
        
        HTapp = (HTApplication) getApplication();
        
        mReceiverDat = new HTReceiverDat();
        mReceiverStat = new HTReceiverStat();
        
        filterDat = new IntentFilter( HTService.NEW_STATUS_INTENT );
        filterStat = new IntentFilter( HTService.NEW_SVCSTAT_INTENT );
        
        Log.i(TAG, "onCreate");
    }
    
    @Override
    protected void onRestart() {
    super.onRestart();
    // Notification that the activity will be started
    Log.i(TAG, "onRestart");
    }

    @Override
    protected void onStart() {
    super.onStart();
    // Notification that the activity is starting
    Log.i(TAG, "onStart");
    }
    
    
    protected void onResume() {
        super.onResume();

        Log.i(TAG, "Activity.onResume");
        // Get Data from DataBox via Application
        this.setupData(); 
        this.setupSVCStatus();
        
        super.registerReceiver(mReceiverDat, filterDat,SEND_GAME_NOTIFICATIONS, null);
        super.registerReceiver(mReceiverStat, filterStat,SEND_SVCSTAT_NOTIFICATIONS, null);
        
        Log.i(TAG, "Activity.onResume - setupList() done");
    }
    
    private void setupData() {
    	mDataBoxData = HTapp.fetchData();
        mTVDataStatus.setText(mDataBoxData);
    }    
    
    private void setupSVCStatus() { 
        // HTapp.isServiceRunning() = true - because onDestroy will start later (=set flag to false)
        mServiceStatus = (HTapp.isServiceRunning()) ? "SVC running" : "SVC NOT running";
        mTVServiceStatus.setText(mServiceStatus);
    }    
    
 // Receiver to wake up when UpdaterService gets a new status
 // It refreshes the Activity list 
    class HTReceiverDat extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
        setupData();
        setupSVCStatus();
        Log.i("HTReceiverDat", "ReceiverDaten.onReceived: setupData() done");
      }
    }
    
    // Receiver to wake up when UpdaterService gets a new status
    // It refreshes the Activity list 
       class HTReceiverStat extends BroadcastReceiver {
         @Override
         public void onReceive(Context context, Intent intent) {
        	 setupSVCStatus();
        	 Log.i("HTReceiverStat", "ReceiverStatus.onReceived: setText() done");
         }
       }
    
    
    protected void onPause() {
        super.onPause();

        // UNregister the receiver
        unregisterReceiver(mReceiverDat);
        unregisterReceiver(mReceiverStat);
        Log.i(TAG, "onPause");
        
      }

    
    
    @Override
    protected void onDestroy() {
//	    Log.i(TAG, "onCreated");
    	super.onDestroy();
    }
    
    public void onClick(View src) {
    	switch (src.getId()) {
        case R.id.buttonStart:
          Log.d(TAG, "onClick: starting service");
          if (HTapp.isServiceRunning()) {
        	  Toast.makeText(this, "Service already running", Toast.LENGTH_LONG).show();
              Log.i(TAG, "Service already running");
              // stopService(new Intent(this, HTService.class));
            } else {
              startService(new Intent(HTActivity.this, HTService.class));
              Log.i(TAG, "Starting Service");
              Toast.makeText(this, "Starting Service", Toast.LENGTH_LONG).show();
            }          
          // TODO Service Aufruf
          break;
        case R.id.buttonStop:
          Log.d(TAG, "onClick: stopping service");
          if (HTapp.isServiceRunning()) {
        	stopService(new Intent(HTActivity.this, HTService.class));
        	Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
            Log.i(TAG, "Stopping Service");
          } else {
          	  Toast.makeText(this, "No service running", Toast.LENGTH_LONG).show();
          	  Log.i(TAG, "No service running");
            }
          // TODO Service Destroy
          break;
        }
        // this.setupList();
      }
    
}